package com.raowb.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.util.StringUtils;

public class DateUtil extends cn.hutool.core.date.DateUtil {

	public final static String YYYY = "yyyy";

	public final static String YYYY_MM = "yyyy-MM";

	public final static String YYYY_MM_DD = "yyyy-MM-dd";
	/**
	 * 无分隔符日期格式 "yyyyMMddHHmmssSSS"
	 */
	public final static String YYYYMMDDHHMMSS = "yyyyMMddHHmmssSSS";
	/**
	 * 时间格式(yyyy-MM-dd HH:mm:ss)
	 */
	public final static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	// 日期转换格式数组
	public static String[][] regularExp = new String[][] {

			// 默认格式
			{ "\\d{4}-((([0][1,3-9]|[1][0-2]|[1-9])-([0-2]\\d|[3][0,1]|[1-9]))|((02|2)-(([1-9])|[0-2]\\d)))\\s+([0,1]\\d|[2][0-3]|\\d):([0-5]\\d|\\d):([0-5]\\d|\\d)",
					YYYY_MM_DD_HH_MM_SS },
			// 仅日期格式 年月日
			{ "\\d{4}-((([0][1,3-9]|[1][0-2]|[1-9])-([0-2]\\d|[3][0,1]|[1-9]))|((02|2)-(([1-9])|[0-2]\\d)))",
					YYYY_MM_DD },
			// 带毫秒格式
			{ "\\d{4}((([0][1,3-9]|[1][0-2]|[1-9])([0-2]\\d|[3][0,1]|[1-9]))|((02|2)(([1-9])|[0-2]\\d)))([0,1]\\d|[2][0-3])([0-5]\\d|\\d)([0-5]\\d|\\d)\\d{1,3}",
					YYYYMMDDHHMMSS } };

	public static String format(Date date) {
		return format(date, YYYY_MM_DD);
	}

	public static String format(Date date, String pattern) {
		if (date != null) {
			SimpleDateFormat df = new SimpleDateFormat(pattern);
			return df.format(date);
		}
		return null;
	}

	public static String timeToStr(Long time, String pattern) {
		SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
		if (time.toString().length() < 13) {
			time = time * 1000L;
		}
		Date date = new Date(time);
		String value = dateFormat.format(date);
		return value;
	}

	public static long strToTime(String timeStr) {
		Date time = strToDate(timeStr);
		return time.getTime() / 1000;
	}

	/**
	 * 转换为时间类型格式
	 *
	 * @param strDate 日期
	 * @return
	 */
	public static Date strToDate(String strDate) {
		try {
			String strType = getDateFormat(strDate);
			SimpleDateFormat sf = new SimpleDateFormat(strType);
			return new Date((sf.parse(strDate).getTime()));
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 根据传入的日期格式字符串，获取日期的格式
	 *
	 * @return 秒
	 */
	public static String getDateFormat(String date_str) {
		String style = null;
		if (StringUtils.isEmpty(date_str)) {
			return null;
		}
		boolean b = false;
		for (int i = 0; i < regularExp.length; i++) {
			b = date_str.matches(regularExp[i][0]);
			if (b) {
				style = regularExp[i][1];
			}
		}
		if (StringUtils.isEmpty(style)) {
			throw new IllegalArgumentException("日期格式获取出错，未识别的日期格式:" + date_str);
		}
		return style;
	}

	/**
	 * 将字符串类型的转换成Date类型
	 *
	 * @param dateStr
	 *            字符串类型的日期 yyyy-MM-dd
	 * @return Date类型的日期
	 * @throws ParseException
	 */
	public static Date convertStringToDate(String dateStr) {
		// 返回的日期
		Date resultDate = null;
		try {
			// 日期格式转换
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			resultDate = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return resultDate;
	}

	/**
	 * 计算两个时间差
	 */
	public static String betweenFormat(Date endDate, Date nowDate) {
		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 获得两个时间的毫秒时间差异
		long diff = endDate.getTime() - nowDate.getTime();
		// 计算差多少天
		long day = diff / nd;
		// 计算差多少小时
		long hour = diff % nd / nh;
		// 计算差多少分钟
		long min = diff % nd % nh / nm;
		// 计算差多少秒//输出结果
		// long sec = diff % nd % nh % nm / ns;
		return day + "天" + hour + "小时" + min + "分钟";
	}
	
	public static String getCurrentTime() {
		return DateUtil.format(new Date(),DateUtil.YYYY_MM_DD_HH_MM_SS);
	}

	public static void main(String[] args) {
		Date date = new Date();
		String str = "20170818223629599";
		System.out.println(DateUtil.getDateFormat(str));
	}
}
